const couponModel = require('../models/couponModel');


module.exports.insert = async function(coupon){
    return await couponModel.create(coupon);
}

module.exports.find = async function(page,searchParams){
    let {current,pageSize} = page;
    console.log(current,pageSize)
    // 根据查询条件获取总数
    let total = await couponModel.countDocuments(searchParams);

    // 根据查询条件获取具体数量的数据
    let data = await couponModel.find(searchParams)
                    .limit(pageSize)
                    .skip((current - 1) * pageSize);

    return {
        current,pageSize,total,rows:data,
    }
    
}

module.exports.findById = async function(id){
    return await couponModel.findById(id);
}

module.exports.delete = async function(id){
    return await couponModel.deleteOne({_id:id});
}

module.exports.update = async function(coupon){
    return await couponModel.updateOne({_id:coupon._id},coupon);
}
